﻿unit UCategoryEdit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons, Data.DB, Vcl.Grids, Vcl.DBGrids;

type
  TCategoryEditForm = class(TForm)
    pnlTop: TPanel;
    pnlMain: TPanel;
    pnlBottom: TPanel;
    btnAdd: TBitBtn;
    btnEdit: TBitBtn;
    btnDelete: TBitBtn;
    btnClose: TBitBtn;
    dbgCategories: TDBGrid;
    
    procedure FormCreate(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure dbgCategoriesDblClick(Sender: TObject);
  private
    procedure LoadCategories;
    function EditCategory(ID: Integer = 0): Boolean;
  public
    class procedure ShowCategoryManager;
  end;

implementation

{$R *.dfm}

uses UDataModule;

{ TCategoryEditForm }

class procedure TCategoryEditForm.ShowCategoryManager;
var
  Form: TCategoryEditForm;
begin
  Form := TCategoryEditForm.Create(nil);
  try
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

procedure TCategoryEditForm.FormCreate(Sender: TObject);
begin
  LoadCategories;
end;

procedure TCategoryEditForm.LoadCategories;
begin
  with DM.qryCategory do
  begin
    Close;
    SQL.Text := 'SELECT * FROM Category ORDER BY Name';
    Open;
  end;
end;

function TCategoryEditForm.EditCategory(ID: Integer = 0): Boolean;
var
  Name, Description: string;
begin
  Result := False;
  
  if ID > 0 then
  begin
    with DM.qryCategory do
    begin
      Name := FieldByName('Name').AsString;
      Description := FieldByName('Description').AsString;
    end;
  end
  else
  begin
    Name := '';
    Description := '';
  end;
  
  if not InputQuery('分类管理', '分类名称:', Name) then
    Exit;
    
  if not InputQuery('分类管理', '分类描述:', Description) then
    Exit;
    
  if Trim(Name) = '' then
  begin
    ShowMessage('分类名称不能为空');
    Exit;
  end;
  
  // 检查分类名称是否重复
  with DM.qryTemp do
  begin
    Close;
    SQL.Text := 'SELECT COUNT(*) AS CNT FROM Category ' +
                'WHERE Name = :Name AND ID <> :ID';
    Parameters.ParamByName('Name').Value := Name;
    Parameters.ParamByName('ID').Value := ID;
    Open;
    
    if FieldByName('CNT').AsInteger > 0 then
    begin
      ShowMessage('分类名称已存在');
      Exit;
    end;
  end;
  
  // 保存分类
  with DM.qryCategory do
  begin
    if ID = 0 then
    begin
      Close;
      SQL.Text := 'INSERT INTO Category (Name, Description) VALUES (:Name, :Description)';
    end
    else
    begin
      Close;
      SQL.Text := 'UPDATE Category SET Name = :Name, Description = :Description WHERE ID = :ID';
      Parameters.ParamByName('ID').Value := ID;
    end;
    
    Parameters.ParamByName('Name').Value := Name;
    Parameters.ParamByName('Description').Value := Description;
    ExecSQL;
  end;
  
  Result := True;
end;

procedure TCategoryEditForm.btnAddClick(Sender: TObject);
begin
  if EditCategory then
    LoadCategories;
end;

procedure TCategoryEditForm.btnEditClick(Sender: TObject);
begin
  if DM.dsCategory.DataSet.IsEmpty then
    Exit;
    
  if EditCategory(DM.dsCategory.DataSet.FieldByName('ID').AsInteger) then
    LoadCategories;
end;

procedure TCategoryEditForm.btnDeleteClick(Sender: TObject);
begin
  if DM.dsCategory.DataSet.IsEmpty then
    Exit;
    
  // 检查分类是否被使用
  with DM.qryTemp do
  begin
    Close;
    SQL.Text := 'SELECT COUNT(*) AS CNT FROM Memo WHERE CategoryID = :ID';
    Parameters.ParamByName('ID').Value := DM.dsCategory.DataSet.FieldByName('ID').AsInteger;
    Open;
    
    if FieldByName('CNT').AsInteger > 0 then
    begin
      ShowMessage('该分类下存在备忘录，无法删除');
      Exit;
    end;
  end;
  
  if MessageDlg('确定要删除该分类吗？', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    with DM.qryCategory do
    begin
      Close;
      SQL.Text := 'DELETE FROM Category WHERE ID = :ID';
      Parameters.ParamByName('ID').Value := DM.dsCategory.DataSet.FieldByName('ID').AsInteger;
      ExecSQL;
    end;
    
    LoadCategories;
  end;
end;

procedure TCategoryEditForm.dbgCategoriesDblClick(Sender: TObject);
begin
  btnEdit.Click;
end;

end. 